import Vue from "vue";
import _ from "lodash";
import Router from "vue-router";
import SignIn from "@/views/SignIn";
import store from "@/store";
import Examinations from "@/views/Examinations";

Vue.use(Router);

const BasicLayout = () => import("@/views/layout/BasicLayout.vue");

export const constantRouter = [
  { path: "/", name: "signIn", component: SignIn, meta: { anonymous: true } },
  { path: "/Examinations", name: "Examinations", component: BasicLayout, children:[{
    path:"/",
    name:"Examinations",
    component:Examinations,
    meta: { anonymous:true}
  }]}
];

export const asyncRouter = [
  // {
  //   path: "/dashboard",
  //   component: BasicLayout,
  //   children: [
  //     {
  //       path: "/",
  //       name: "dashboard",
  //       component: () => import("@/views/Dashboard.vue"),
  //       meta: { menuName: "概览" }
  //     }
  //   ]
  // },
  {
    path: "/system",
    component: BasicLayout,
    children: [
      {
        path: "/system/profile",
        name: "profile",
        component: () => import("@/views/system/Profile.vue")
      },
      {
        path: "/system/user",
        name: "user",
        component: () => import("@/views/system/User.vue")
      },
      {
        path:"/system/notice",
        name:"notice",
        component: () => import("@/views/system/notice.vue")
      }
    ]
  },
  {
    path: "/classManager",
    component: BasicLayout,
    children: [
      {
        path: "/",
        name: "classManager",
        component: () => import("@/views/classManager.vue"),
        meta: { menuName: "班级"}
      }
    ]
  }
  ,{
    path: "/member",
    component: BasicLayout,
    children: [
      {
        path: "/member/teacherManager",
        name: "teacherManager",
        component: () => import("@/views/member/teacherManager.vue"),
        meta: { menuName: "教师管理"}
      }
      ,
      {
        path: "/member/studentManager",
        name: "studentManager",
        component: () => import("@/views/member/studentManager.vue"),
        meta: { menuName : "学生管理"}
      }
    ]
  },
  {
    path: "/examManager",
    component: BasicLayout,
    children: [
      {
        path: "/",
        name: "examManager",
        component: () => import("@/views/examManager"),
        meta: { menuName: "考试"}
      }
    ]
  }
  ,
  {
    path: "/paper",
    component: BasicLayout,
    children: [
      {
        path: "/paper/testPaperManager",
        name: "testPaperManager",
        component: () => import("@/views/paper/testPaperManager.vue"),
        meta: { menuName: "出卷管理"}
      },
      {
        path: "/paper/correctionPaperManager",
        name: "correctionPaperManager",
        component: () => import("@/views/paper/correctionPaperManager.vue"),
        meta: { menuName: "批卷管理"}
      }
    ]
  },
  {
    path: "/testQuestionManager",
    component: BasicLayout,
    children: [
      {
        path: "/",
        name: "testQuestionManager",
        component: () => import("@/views/testQuestionManager"),
        meta: { menuName: "试题"}
      }
    ]
  }
  ,
  {
    path: "/scoreManager",
    component: BasicLayout,
    children: [
      {
        path: "/",
        name: "scoreManager",
        component: () => import("@/views/scoreManager"),
        meta: { menuName: "成绩"}
      }
    ]
  }
  ,{
    path: "/goExamination",
    component: BasicLayout,
    children: [
      {
        path: "/",
        name: "goExamination",
        component: () => import("@/views/goExamination")
      }
    ]
  }
];

const router = new Router({ routes: constantRouter });

// 是否有权限访问某个路径
const hasPermission = toPath => {
  const links = _.map(store.state.menus, "link");
  return _.indexOf(links, toPath) > 0;
};

// 根据菜单生成路由的菜单元数据
const generateMenuName = route => {
  const menu = _.find(store.state.menus, { link: route.path });
  if (!menu) {
    return;
  }
  const menuName = menu.menuName;
  if (route.meta) {
    route.meta.menuName = menuName;
  } else {
    route.meta = { menuName };
  }
  _.forEach(route.children, child => {
    generateMenuName(child);
  });
};

// 动态生成路由表
const generateRoutes = () =>
  _.filter(asyncRouter, route => {
    const flag = hasPermission(route.path);
    if (flag) {
      generateMenuName(route);
    }
    return flag;
  });

// 导航之前拦截器
router.beforeEach((to, from, next) => {
  if (to.meta.anonymous) {
    next();
    return;
  }

  // if (!to.meta.anonymous) {
  //   next();
  //   return;
  // }
  // 没有Token，尝试从本地存储中重新加载Token
  if (store.state.token == null) {
    const token = localStorage.getItem("token");
    if (token != null && token.toUpperCase() !== "NULL") {
      store.commit("setToken", token);
    }
  }
  // next();
  // 有Token
  if (store.state.token != null) {
    // 没有菜单信息，重新从服务器加载菜单
    if (store.state.menus.length === 0) {
      if(store.state.role && store.state.role == '教师')
      {
        store.dispatch("reloadTeacherUserAuthority").then(() => {
          const routes = generateRoutes();
          router.addRoutes(routes);
          next({ ...to, replace: true });
        }).chatch(() => {
          next("/")
        });
      }else if (store.state.role && store.state.role == '学生')
      {
        store.dispatch("reloadStudentUserAuthority").then(() => {
          const routes = generateRoutes();
          router.addRoutes(routes);
          next({ ...to, replace: true });
        }).chatch(() => {
          next("/")
        });
      }else {
        store
        .dispatch("reloadUserAuthority")
        .then(() => {
          const routes = generateRoutes();
          router.addRoutes(routes);
          next({ ...to, replace: true });
        })
        .catch(() => {
          next("/");
        });
      }
    } else if (hasPermission(to.path)) {
      next();
    } else {
      // 无权限访问，返回到登陆页面
      next("/");
    }
  } else {
    // 本地没有Token，需要重新登陆
    next("/");
  }
});

export default router;
